package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.vo.CartBox;
import com.study.services.GoodsService;
import com.study.services.OffersDetailsService;
import com.study.services.OffersService;
import com.study.services.WeixiuService;

@Controller
public class OfferDetailsController {
	@Autowired
	OffersDetailsService service;
	@Autowired
	GoodsService goodsService;

	@Autowired
	OffersService offerService;

	/**
	 * 查看维修（第一次进入）
	 */
	@RequestMapping("offerdetailty")
	@ResponseBody
	public String service1() {
		List<JSONObject> list = service.selectBySel(1, 10, "");
		// pagerhelper自定的PageInfo类.会把ArrayList的信息，整合到PageInfo对象中的list属性中。JSON亦然
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		System.out.println(json);
		return json;
	}

	/**
	 * 获客采集分页+全部+带条件查询 ty
	 * 
	 * @return
	 */
	@PostMapping("offerdetail")
	@ResponseBody
	public String getForm3(String input, int currentPage, int pagesize) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(input);

		List<JSONObject> list = service.selectBySel(currentPage, pagesize, input.trim());

		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);

		System.out.println("我是价格单里面的带条件查询");
		for (JSONObject jsonObject : list) {
			System.out.println(jsonObject);
		}
		return json;
	}

	/**
	 * 转成订单
	 */
	@PostMapping("toOrder")
	@ResponseBody
	public int toOrder(String id) {
		return service.toOrder(id);
	}

	/**
	 * 设置用户联系地址
	 */

	@PostMapping("areaAll")
	@ResponseBody
	public String getArea(String adress, String id, HttpServletRequest request) {
		System.out.println(adress + id + "--------");
		return service.setAd(adress, id);
	}

	/**
	 * 添加商品
	 */
	@RequestMapping("offerGoodDetail")
	public ModelAndView goodsMx(HttpSession session) {
		ModelAndView mv = new ModelAndView("beforeSale/offerOrderdetail");
		JSONObject jsonObj = (JSONObject) session.getAttribute("OfferBoxAllJson");

		if (jsonObj == null) {
			session.setAttribute("OfferBoxAllJson", new JSONObject());
		} else {
			session.setAttribute("OfferBoxAllJson", jsonObj);
		}

		System.out.println("我是编辑的");

		return mv;
	}

	/**
	 * 添加一个商品到订单明细中
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("addofferToCart")
	@ResponseBody
	public CartBox addGoods(int ids, HttpSession session) {
		System.out.println("oid" + ids);
		CartBox cart = (CartBox) session.getAttribute("OfferBoxAll");
		if (cart == null) {
			cart = new CartBox();
		}
		cart.addProduct(goodsService.findByGoodsIdByVo(ids));
		System.out.println("当前购物车...：" + cart.getMap());

		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		session.setAttribute("OfferBoxAll", cart);
		session.setAttribute("OfferBoxAllJson", jsonObj);
		return cart;
	}

	/**
	 * 修改商品数量
	 * 
	 * @param ids
	 * @param quatitys
	 * @param session
	 * @return
	 */
	@PostMapping("updateofferToCart")
	@ResponseBody
	public CartBox updateGoodsQuatitys(int ids, int quatitys, HttpSession session) {

		System.out.println("oid：" + ids + " quatitys：" + quatitys);
		CartBox cart = (CartBox) session.getAttribute("OfferBoxAll");
		cart.modifyproductQuatity(ids, quatitys, cart);

		System.out.println("修改后当前购物车：" + cart.getMap());
		session.setAttribute("OfferBoxAll", cart);
		return cart;
	}

	/**
	 * 删除商品信息
	 * 
	 * @param ids
	 * @param session
	 * @return
	 */
	@PostMapping("deleteofferToCart")
	@ResponseBody
	public CartBox deleteGoods(int ids, HttpSession session) {
		System.out.println("oid" + ids);
		CartBox cart = (CartBox) session.getAttribute("OfferBoxAll");
		cart.deleteProduct(ids, cart);
		System.out.println("当前购物车：" + cart.getMap());
		session.setAttribute("OfferBoxAll", cart);
		return cart;
	}

	/**
	 * 提交订单明细 并跳转回订单详情页面
	 * 
	 * @param ids
	 * @param quatitys
	 * @param session
	 * @return
	 * @return
	 */
	@PostMapping("submitOfferDetails")
	@ResponseBody
	public String submitGoods(HttpSession session) {
		CartBox cart = (CartBox) session.getAttribute("OfferBoxAll");
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		session.setAttribute("OfferBoxAllJson", jsonObj);
		session.setAttribute("OfferBoxAll", cart);
		return "ok";
	}

	/**
	 * 回显到编辑页
	 */
	@RequestMapping("offer2")
	public ModelAndView twos2(Integer oid, HttpSession session) {
		ModelAndView mv = new ModelAndView("beforeSale/temp");
		return mv;
	}

	@RequestMapping("/addoffersss")
	public ModelAndView twos3(Integer oid, HttpSession session) {
		ModelAndView mv = new ModelAndView("beforeSale/priceadd");
		CartBox cart = (CartBox) session.getAttribute("OfferBoxAll");
		JSONObject jsonObj  = (JSONObject) session.getAttribute("OfferBoxAllJson");
		
		session.setAttribute("OfferBoxAll", cart);
		session.setAttribute("OfferBoxAllJson", jsonObj);
		System.out.println(cart+"我要回到报价啦");
		return mv;
	}

}
